-- 掉落兑换 活动

local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local BattleLogic = require("battle.BattleLogic")
local EquipLogic = require("equip.EquipLogic")
local PremiumGiftLogic = require("absAct.PremiumGiftLogic")

function query(human, id , isHandler)
    local config = AbsActExcel.absActivity[id]
    if not config then return end

    local actId = config.actId
    local msgRet = Msg.gc.GC_ABS_ACT_DROP_QUERY
    local dropId = 0
    
    for k, v in pairs(AbsActExcel.drop) do
        if v and v.actID == actId then
            dropId = v.dropItem[1]
            break
        end
    end
    Grid.makeItem(msgRet.drop, dropId, getJifen(human, id))
    Msg.send(msgRet, human.fd)

    -- 客户端主动请求的时候  另外俩个活动也发送给客户端  因为客户端要做一个 极流畅的 特效切换  不能等主动请求返回
    if isHandler  then
        shopQuery(human)
        for k, v in pairs(AbsActExcel.absActivity) do
            if v and v.absType == config.absType and 
               v.type == AbsActDefine.ABS_ACT_TYPE_1 and 
               AbsActLogic.isStarted(human, k) then
                AbsActLogic.checkAbsActClean(human, k)
                PremiumGiftLogic.getAndSendMsg(human, k, v.actId)
                break
            end
        end        
    end
end

function addJifen(human, value)
    if not value or value <= 0 then return end
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    absAct.jifen = absAct.jifen or 0
    absAct.jifen = absAct.jifen + value
    query(human, id)
end

function delJifen(human, value)
    if not value or value <= 0 then return end
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    absAct.jifen = absAct.jifen or 0
    absAct.jifen = absAct.jifen - value
    absAct.jifen = absAct.jifen > 0 and absAct.jifen or 0
    query(human, id)
end

function getJifen(human, id)
    local absAct = human.db.absAct[id]
    return absAct and absAct.jifen or 0
end

function shopQuery(human)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absActConfig = AbsActExcel.absActivity[id]
    if not absActConfig then return end

    local actId = absActConfig.actId
    local absAct = human.db.absAct[id]

    local msgRet = Msg.gc.GC_ABS_ACT_DROP_SHOP_QUERY
    msgRet.templateId = absActConfig.adIcon

    local len = 0
    local needItemID = 0
    for k, config in pairs(AbsActExcel.dropShop) do
        if config and config.actID == actId then
            needItemID = config.needItem
            len = len + 1 
            local net = msgRet.list[len]
            net.id = k
            net.need = config.need
            net.buyCnt = absAct.buyCnt and absAct.buyCnt[k] or 0
            net.maxBuy = config.limit
            Grid.makeItem(net.item, config.item[1], config.item[2])   
        end
    end
    msgRet.list[0] = len
    Grid.makeItem(msgRet.need,  needItemID, 1)
    Msg.send(msgRet, human.fd)
end

function shopBuy(human, shopID, buyCnt)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
    local absAct = human.db.absAct[id]
    local config = AbsActExcel.dropShop[shopID] 
    if not config then return end 
    if config.actID ~= actId then return end

    local oldBuy = absAct.buyCnt and absAct.buyCnt[shopID] or 0 

    if config.limit > 0 and oldBuy + buyCnt > config.limit then return end

    if getJifen(human, id) < config.need * buyCnt then
        Broadcast.sendErr(human, Util.format(Lang.COMMON_NO_ITEM, ItemDefine.getValue(config.needItem, "name"))) 
        return 
    end

    delJifen(human, config.need * buyCnt)
    absAct.buyCnt = absAct.buyCnt or {}
    absAct.buyCnt[shopID] = absAct.buyCnt[shopID] or 0
    absAct.buyCnt[shopID] = absAct.buyCnt[shopID] + buyCnt
    BagLogic.addItem(human, config.item[1],  config.item[2] * buyCnt,"abs_dropShop")

    local msgRet = Msg.gc.GC_ABS_ACT_DROP_SHOP_BUY
    msgRet.id = shopID
    if not ItemDefine.isEquip(config.item[1]) then
        Grid.makeItem(msgRet.item ,   config.item[1],  config.item[2] * buyCnt)
    else
        EquipLogic.makeEquipItemOne(human, msgRet.item)
    end
   
    Msg.send(msgRet, human.fd)

 --   shopQuery(human)
end

function giftQuery(human)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
    local msgRet = Msg.gc.GC_ABS_ACT_DROP_GIFT_QUERY
    local len = 0
    for k, config in pairs(AbsActExcel.premiumGift) do
        if config and config.actId == actId then
            len = len + 1
            local net = msgRet.list[len]
            net.id = k            
            net.cnt = human.db.absAct[id].premiumCnt and human.db.absAct[id].premiumCnt[k] or 0
            net.maxCnt = config.cnt
            for j = 1, #config.reward do
                Grid.makeItem(net.item[j], config.reward[j][1], config.reward[j][2])
            end
            net.item[0] = #config.reward
            BuyLogic.fontBuyItem(human,net.buyMsg, config.buyID)
          
        end
    end
    msgRet.list[0] = len
    Msg.send(msgRet, human.fd)
end


function drawSSR(human, net, cnt)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
    for k, config in pairs(AbsActExcel.drop) do
        if config and config.actID == actId then
            BagLogic.addItem(human, config.ssrDrop[1], config.ssrDrop[2] * cnt, "abs_dropSSR")
            net[config.ssrDrop[1]] = net[config.ssrDrop[1]] or 0
            net[config.ssrDrop[1]] = net[config.ssrDrop[1]] + config.ssrDrop[2] * cnt
            break
        end
    end
end

function getDropItem(human,outSec, time ,net)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
    local now = os.time()
	local thisStart = now - outSec
	for i, config in pairs(AbsActExcel.drop) do
        if config and config.actID == actId then
            local absAct = human.db.absAct[id]
            local outCnt = 0
            local thisSec  = outSec

            if absAct.dropTime then
                thisSec = now - absAct.dropTime
                local maxHang = BattleLogic.getHangMaxTime(human)
                thisSec = thisSec < maxHang and thisSec or maxHang
            elseif  thisStart < starTime then					
                thisSec = outSec - (starTime - thisStart)
            end
            
            if thisSec > 0 then
                outCnt = math.floor(thisSec/config.dropTime)
            end
            if outCnt > 0 then	
                for j = 1, outCnt do
                    local itemID = config.dropItem[1]
                    local itemCnt = config.dropItem[2]
                    net.items = net.items or {}
                    net.items[itemID] = net.items[itemID] or 0
                    net.items[itemID] = net.items[itemID] + itemCnt
                    absAct.dropTime = os.time()                       
                end
            end	              
        end
    end
end


function getDropItemSaoQuery(human, net, dropCnt)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return dropCnt end
    AbsActLogic.checkAbsActClean(human, id)
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId

	
	for i, config in pairs(AbsActExcel.drop) do
        if config and config.actID == actId then
            local itemID = config.dropItem[1]
            local itemCnt = config.dropItem[2]
            dropCnt = dropCnt + 1
            Grid.makeItem(net.item[dropCnt], itemID, 1)
            break                    
        end
    end

    return dropCnt
end

function getDropItemSao(human,time ,itemTable)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end
    AbsActLogic.checkAbsActClean(human, id)
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
	for i, config in pairs(AbsActExcel.drop) do
        if config and config.actID == actId then
            local outCnt = math.floor(time/config.dropTime)
            if outCnt > 0 then	
                for j = 1, outCnt do
                    local itemID = config.dropItem[1]
                    local itemCnt = config.dropItem[2]
                  
                    itemTable[itemID] = itemTable[itemID] or 0
                    itemTable[itemID] = itemTable[itemID] + itemCnt        
                end
            end	        
        end
    end

end


function getAbsCanDrop(human)
    local state,id,endTime, starTime = AbsActLogic.isStartedByType(human,  AbsActDefine.ABS_ACT_TYPE_6)
    if not state then return end

    local list 
    local have = {}
    local absActConfig = AbsActExcel.absActivity[id]
    local actId = absActConfig.actId
    for i, config in pairs(AbsActExcel.drop) do
        if config and config.actID == actId then
            list = list or {}
            list[config.dropItem[1]] = config.dropItem[2]
            break   
        end
    end
    return list
end

function isRed(human, YYInfo, funcConfig)
 --   if funcConfig.adIcon == 1 then return end
 --   local state,endTime, starTime = AbsActLogic.isStarted(human, funcConfig.funcID)
 --   if not state then return end
 --   return true
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end